<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
    <style>
        .search-btn {
            margin-left: 20px;
        }

        .tableBar {
            justify-content: flex-start !important;
        }

        .info-box {
            margin: -15px -44px 20px;
        }

        .info-box .item-box {
            display: flex;
            height: 20px;
            line-height: 20px;
            font-size: 14px;
            font-weight: 400;
            color: #666666;
            text-align: left;
            margin-bottom: 14px;
        }

        .info-box .item-box:last-child {
            margin-bottom: 0;
        }

        .info-box .item-box .label {
            width: 96px;
        }

        .info-box .item-box .des {
            flex: 1;
            color: #333333;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="userorder-app" v-loading="loading">
    <div class="container">
        <el-table :data="tableData" stripe class="tableBox">
            <el-table-column prop="number" label="订单号" min-width="110"></el-table-column>
            <el-table-column prop="订单状态" label="订单状态">
                <template slot-scope="{ row }">
                    <span>{{ getOrderType(row) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="userName" label="用户"></el-table-column>
            <el-table-column prop="orderTime" label="预计送达时间" min-width="100"></el-table-column>
            <el-table-column prop="amount" label="支付金额">
                <template slot-scope="{ row }">
                    <span>￥{{ row.amount }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="btn" label="操作">
                <template slot-scope="{ row }">
                    <el-button type="text" @click="getDetail(row.number)" class="blueBug">
                        查看
                    </el-button>
                    <el-divider v-if="row.status === 1" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 1" type="text" @click="setStatus('取消订单',3,row.number)"
                               class="blueBug">
                        取消订单
                    </el-button>
                    <el-divider v-if="row.status === 3" direction="vertical"></el-divider>
                    <el-button v-if="row.status === 3" type="text" @click="setStatus('重新订单',1,row.number)"
                               class="blueBug">
                        重新订单
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                :current-page.sync="page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>

    <!-- 查看弹框部分 -->
    <el-dialog
            title="购物车"
            :visible.sync="dialogVisible"
            width="60%"
            :before-close="handleClose"
    >
        <el-table :data="orderdetail">
            <el-table-column prop="name" label="菜品名称"></el-table-column>
            <el-table-column prop="image" label="图片" align="center">
                <template slot-scope="{ row }">
                    <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                              :src="getImage(row.image)"
                              :preview-src-list="[ `/common/download?name=${row.image}` ]">
                        <div slot="error" class="image-slot">
                            <img src="./../../images/noImg.png" style="width: auto; height: 40px; border:none;">
                        </div>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column prop="number" label="数量"></el-table-column>
            <el-table-column label="售价">
                <template slot-scope="scope">
                    <span style="margin-right: 10px;">￥{{ scope.row.amount }}</span>
                </template>
            </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button size="medium">已支付:{{sum}}</el-button>
          <el-button type="primary" size="medium" @click="dialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>

</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/order.js"></script>
<script>
    new Vue({
        el: '#userorder-app',
        data() {
            return {
                input: '',
                orderTime: '',
                beginTime: '',
                endTime: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData: [],
                dialogVisible: false,
                diaForm: {},
                loading: false,
                orderdetail:[],
                sum:0,
            }
        },
        computed: {},
        watch: {
            orderTime(val) {
                if (val && val.length >= 2) {
                    this.beginTime = val[0]
                    this.endTime = val[1]
                } else {
                    this.beginTime = ''
                    this.endTime = ''
                }
            }
        },
        created() {
            this.init()
        },
        mounted() {
            // this.loading = false
        },
        methods: {
            async init() {
                getOrderDetailPage({
                    page: this.page,
                    pageSize: this.pageSize,
                    number: this.input || undefined,
                    userSign:1,
                    beginTime: this.beginTime || undefined,
                    endTime: this.endTime || undefined
                }).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            getOrderType (row) {
                let str = ''
                switch(row.status){
                    case 1:
                        str =  '正在派送'
                        break;
                    case 2:
                        str =  '已派送'
                        break;
                    case 3:
                        str =  '已取消'
                        break;
                }
                return str
            },
            // 查看详情
            getDetail(number) {
                this.dialogVisible =true
                orderDetail(number).then(res => {
                    if (res.code === 1) {
                       this.orderdetail = res.data
                        this.sum=0
                        for (let i = 0; i < this.orderdetail.length; i++) {
                            this.sum += this.orderdetail[i].amount;
                        }
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            },
            setStatus(a,status, number){
              if (a=="取消订单"){
                  this.$confirm('确认取消订单?', '提示', {
                      'confirmButtonText': '确定',
                      'cancelButtonText': '取消',
                      'type': 'warning'
                  }).then(() => {
                      editOrderDetail(params).then(res => {
                          if (res.code === 1) {
                              this.$message.success("取消订单成功，已退款")
                              this.init()
                          } else {
                              this.$message.error(res.msg || '操作失败')
                          }
                      })
                  })
                  }else{
                  this.$confirm('确认重新订单?', '提示', {
                      'confirmButtonText': '确定',
                      'cancelButtonText': '取消',
                      'type': 'warning'
                  }).then(() => {
                      editOrderDetail(params).then(res => {
                          if (res.code === 1) {
                              this.$message.success("取消订单成功，已退款")
                              this.init()
                          } else {
                              this.$message.error(res.msg || '操作失败')
                          }
                      })
                  })
              }
                const params = {
                    status,
                    number
                }
            },
            // 关闭弹窗
            handleClose() {
                this.dialogVisible = false
            },
            getImage(image) {
                return `/common/download?name=${image}`
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            }
        }
    })
</script>
</body>
</html>